from flask_cors import CORS
from flask import Flask,request, jsonify
import os
import sqlite3


app = Flask(__name__)
CORS(app)

app.config["JSON_AS_ASCII"] = False


def init_env():
    """初始化文件夹"""
    folder_path = '/home/files/xhq' # 主文件夹路径
    os.path.exists(folder_path) or os.mkdir(folder_path)
    folder_path = '/home/files/gry' # 主文件夹路径
    os.path.exists(folder_path) or os.mkdir(folder_path)
    folder_path = '/home/files/jjj' # 主文件夹路径
    os.path.exists(folder_path) or os.mkdir(folder_path)
    folder_path = '/home/files/jjj/feedback' # 主文件夹路径
    os.path.exists(folder_path) or os.mkdir(folder_path)

def create_table():
    # 创建数据库表
    conn = sqlite3.connect('contest.db')
    c = conn.cursor()

    # 比赛通知
    sql = '''CREATE TABLE IF NOT EXISTS msg
                (id INTEGER PRIMARY KEY,
                content TEXT NOT NULL,
                time TEXT NOT NULL,
                type TEXT NOT NULL,
                pages INTEGER NOT NULL
                )'''
    
    # 通知
    sql2 = '''CREATE TABLE IF NOT EXISTS notice
                (id INTEGER PRIMARY KEY AUTOINCREMENT,
                content TEXT NOT NULL,
                time TEXT NOT NULL
                )'''
    
    # 反馈
    sql3 = '''CREATE TABLE IF NOT EXISTS feedback
                (id INTEGER PRIMARY KEY,
                content TEXT NOT NULL,
                time TEXT NOT NULL,
                type TEXT NOT NULL,
                pages INTEGER NOT NULL,
                status TEXT NOT NULL
                )'''
    
    # 活动表
    sql4 = '''CREATE TABLE IF NOT EXISTS activity
                (id INTEGER PRIMARY KEY AUTOINCREMENT,
                title TEXT NOT NULL,
                img TEXT NOT NULL,
                content TEXT NOT NULL,
                type TEXT NOT NULL,
                start TEXT NOT NULL,
                end TEXT NOT NULL,
                scope TEXT NOT NULL,
                position TEXT NOT NULL,
                figure TEXT NOT NULL,
                number INTEGER NOT NULL,
                status TEXT NOT NULL,
                pattern TEXT NOT NULL,
                setting TEXT NOT NULL
                )'''

    c.execute(sql)
    c.execute(sql2)
    c.execute(sql3)
    c.execute(sql4)
    conn.commit()
    conn.close()

##
# 测试
##

@app.route('/', methods=['GET'])
def hello_world():
    # 测试接口

    return jsonify({'msg': 'Hello, World!'})

##
# 江俊杰：通知与反馈
##

import misc
app.register_blueprint(misc.api.misc_blue) # 注册蓝图

##
# 许豪强：比赛通知
##

import competition
app.register_blueprint(competition.api.competition_blue) # 注册蓝图


##
# 关日园：第二课堂活动
##

import activity
app.register_blueprint(activity.api.activity_blue) # 注册蓝图


if __name__ == '__main__':
    #=============上线时修改====================================
    init_env()
    create_table()
    app.run(debug=True,host="0.0.0.0",port=5000)